package com.eulog.client.blackJack;

import com.eulog.client.blackJack.BlackJackStrategy.SD;
import com.eulog.client.trump.TrumpCard;

public class BlackJackPlayer extends BlackJackHuman {
	private int initialBet;
	private int currentBet;
	private double totalAmountOfBet;
	private int strategyIndex;
	private int strategyKey;
	private boolean useCount;
	
	public BlackJackPlayer(int money,int initBet,int _strategyIndex,int _strategyKey,boolean _useCount) {
		super(money);
		currentBet = initBet;
		initialBet = initBet;
		strategyIndex = _strategyIndex;
		strategyKey = _strategyKey;
		totalAmountOfBet = 0;
		useCount = _useCount;
	}
	
	@Override
	public String toString() {
		String name;
		name = "Total " + count + "   Win " + won + "   Lose " + lost + "\n";
		name += "Current $" + currentMoney + "\n";
		name += "TotalBet $" + totalAmountOfBet + "   Edge=" + (currentMoney - startingMoney)*100/totalAmountOfBet;
		return name;
	}
	
	@Override
	public BlackJackStrategy.SD getNextDecision(BlackJackStrategy strategy, TrumpCard dealerFaceCard,int splitNo, BlackJackRules rules) {
		BlackJackStrategy.SD decision = strategy.getStrategy(dealerFaceCard, handCardsList.get(splitNo));
		
		/*
		if(Math.abs(strategy.isDecksPositiveForPlayer(strategyIndex))>strategyKey) {
			BlackJackProbability prob = new BlackJackProbability(strategy,handCardsList.get(splitNo),dealerFaceCard,strategyIndex);
			
			if(prob.getDecision() != decision) {
				
				BlackJackHandCards playercards1 = handCardsList.get(splitNo);
				TrumpCard dealercard1 = dealerFaceCard;
				SD playerbetterDecision = prob.getDecision();
				
				if(decision != SD.P) decision = prob.getDecision();
			}
			
			
		}
		*/
		
		/*
		 * Full Count
		 */
		if(useCount && (decision != SD.P) ) {
			BlackJackProbability prob = new BlackJackProbability(strategy,handCardsList.get(splitNo),dealerFaceCard,strategyIndex);
			SD playerbetterDecision = prob.getFullCountDecision();
			SD playerDecision = prob.getDecision();
			if(playerbetterDecision != decision) {
				decision = playerbetterDecision;
			}
			
			if(playerDecision != decision) {
				decision = playerDecision;
			}
//			
////test
//{
//	BlackJackProbability prob2 = new BlackJackProbability(strategy,handCardsList.get(splitNo),dealerFaceCard,strategyIndex);
//}
		}
		
		return decision;
	}
	
	public void setCurrentBetByCountingStrategy(BlackJackStrategy strategy) {
		if(strategy.isDecksPositiveForPlayer(strategyIndex)>strategyKey) setCurrentBet(getInitialBet()*2);
		else setCurrentBet(getInitialBet());
	}
	
	public void setCurrentBet(int bet) {currentBet = bet;}
	public int getCurrentBet() {return currentBet;}
	public int getInitialBet() {return initialBet;}
	public void addTotalAmountOfBet(double bet) {totalAmountOfBet += bet;}
	public double getTotalAmountOfBet() { return totalAmountOfBet;}
	
	public boolean canSplit(int index) {
		if(getSplitCount()>=3 || handCardsList.get(index).isTwoCardsSameRank() == false || getSplitCount()>=maxSplit) return false;

		return true;
	}
	
	public int split(int index) {
		if(handCardsList.get(index).isTwoCardsSameRank()) {
			TrumpCard c = handCardsList.get(index).get(0);
			handCardsList.get(index).clear();
			handCardsList.get(index).add(c);
			splitCount++;
			handCardsList.get(splitCount).add(c);
			
			return splitCount;
		}
		return 0;
	}
	
	public boolean canDoublingDown(int index) {return handCardsList.get(index).canDoublingDown();}
	
	public void doublingDown(int index) {
		if(canDoublingDown(index)) {
			handCardsList.get(index).addAddtionalBet(currentBet);
		}
	}
	
	public double getAdditionalBet(int index) {return handCardsList.get(index).getAdditionalBet();}
}
